package xiaoqi.springboot.boot;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@SpringBootApplication
public class ApplicationHikariMultipleDemo implements CommandLineRunner {

    private Logger logger = LoggerFactory.getLogger(ApplicationHikariMultipleDemo.class);

    @Resource(name = "test1DataSource")
    private DataSource test1DataSource;

    @Resource(name = "testDataSource")
    private DataSource testDataSource;

    @Resource(name = "test3DataSource")
    private DataSource test3DataSource;

    public static void main(String[] args) {
        SpringApplication.run(ApplicationHikariMultipleDemo.class, args);
    }


    @Override
    public void run(String... args) throws Exception {

        // test1 数据源
        try (Connection conn = test1DataSource.getConnection()) {
            // 这里，可以做点什么
            logger.info("[run][ordersDataSource 获得连接：{}]", conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // test 数据源
        try (Connection conn = testDataSource.getConnection()) {
            // 这里，可以做点什么
            logger.info("[run][usersDataSource 获得连接：{}]", conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        // test3 数据源
        try (Connection conn = test3DataSource.getConnection()) {
            // 这里，可以做点什么
            logger.info("[run][usersDataSource 获得连接：{}]", conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}

